<template>
  <el-container>
    <my-theme-card>
      <el-header />
      <el-container>
        <el-main>
          <el-col :span="1"><div class="grid-content bg-purple-light">
            <el-button class="table-header-btn" type="primary" icon="el-icon-plus" size="mini" @click="addClick">新增</el-button>
          </div></el-col>
          <template>
            <el-table :data="noteCategories" stripe border style="width: 100%">
              <el-table-column prop="name" label="分类名称" width="180" />
              <el-table-column prop="create_time" label="创建日期" />
              <el-table-column
                fixed="right"
                label="操作"
                width="180"
              >
                <template slot-scope="scope">
                  <el-button type="text" size="small" @click="editClick(scope.row)">编辑</el-button>
                  <el-button type="text" size="small" @click="deleteClick(scope.row)">删除</el-button>
                </template>
              </el-table-column>

            </el-table>
          </template>
        </el-main>
      </el-container>

      <el-footer>
        <div class="block">
          <el-pagination
            :current-page="page.current"
            :page-sizes="[5,10, 20, 50, 100]"
            :page-size="page.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-footer>

      <el-dialog :title="form.title" :visible.sync="dialogFormVisible" label-width="130px" :close-on-click-modal="false" center append-to-body>
        <el-form :model="tempNoteCategory" label-width="130px" :inline="true">
          <el-form-item label="所属笔记本">
            <el-input v-model="notebook.name" readonly @focus="chooseNoteBookClick" />
          </el-form-item>
          <el-form-item label="分类名称">
            <el-input v-model="tempNoteCategory.name" />
          </el-form-item>
          <el-form-item label="图标">
            <el-input v-model="tempNoteCategory.icon" />
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button v-if="!edit" type="primary" @click="updateTask">确 定</el-button>
          <el-button v-if="edit" type="primary" @click="updateTask">更 新</el-button>
        </div>
      </el-dialog>

      <el-dialog
        title="删除确认"
        :visible.sync="dialogConfirmVisible"
        :close-on-click-modal="false"
        width="30%"
        append-to-body
        center
      >
        <span>{{ deleteMessage }}</span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogConfirmVisible = false">取 消</el-button>
          <el-button type="danger" @click="deleteNoteCategoryTask">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog title="笔记本选择" :visible.sync="dialogNoteBookVisible" :close-on-click-modal="false" center append-to-body>
        <note-book-list @confirm="closeNoteBookDialog" />
      </el-dialog>
    </my-theme-card>
  </el-container>
</template>

<script>
const NoteBookList = () => import('@/components/NoteBookList/index')
import request from '@/utils/request'
export default {
  components: {
    NoteBookList
  },
  data() {
    return {
      edit: false,
      page: {
        current: 1,
        size: 5,
        total: 0
      },
      tempNoteCategory: {
        name: ''
      },
      deleteNoteCategory: {},
      deleteMessage: '确认删除该分类吗？',
      dialogConfirmVisible: false,
      dialogFormVisible: false,
      dialogNoteBookVisible: false,
      form: {
        title: '分类添加'
      },
      notebook: {},
      noteCategories: []
    }
  },
  created() {
    this.query()
  },
  methods: {
    closeNoteBookDialog: function(ids, objs) {
      if (objs) {
        objs.forEach((obj) => {
          this.notebook = obj
        })
      }
      this.dialogNoteBookVisible = false
      this.notebook = this._.clone(this.notebook)
    },
    chooseNoteBookClick: function() {
      this.dialogNoteBookVisible = true
    },
    // 每页显示数目发生变化时
    handleSizeChange: function(val) {
      this.page.size = val
      this.page.current = 1
      this.query()
    },
    // 当前页发生变化时
    handleCurrentChange: function(val) {
      this.page.current = val
      this.query()
    },
    editClick: function(notebook) {
      this.tempNoteCategory = notebook
      this.dialogFormVisible = true
      this.edit = true
    },
    // 添加事件
    addClick: function() {
      this.dialogFormVisible = true
      this.tempNoteCategory = {
        notebook: {}
      }
      this.edit = false
    },
    // 删除事件
    deleteClick: function(notebook) {
      this.dialogConfirmVisible = true
      this.deleteNoteCategory = notebook
    },
    // 添加任务
    updateTask: function() {
      this.$progress.start()
      request({
        url: this.edit ? '/notebook/note_category/editAll' : '/notebook/note_category/addAll',
        method: 'post',
        data: {
          note_category: this.tempNoteCategory,
          notebook: this.notebook
        }
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.dialogFormVisible = false
          this.query()
          this.tempNoteCategory = {}
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }
      ).catch(() => { this.$progress.done() })
    },
    // 删除分类任务
    deleteNoteCategoryTask: function() {
      this.$progress.start()
      request({
        url: '/notebook/note_category/delete',
        method: 'post',
        data: this.deleteNoteCategory
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.dialogConfirmVisible = false
          this.query()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    },
    query: function() {
      this.$progress.start()
      request({
        url: '/notebook/note_category/query',
        method: 'post',
        data: this.page
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.page.total = res.data.total
          this.noteCategories = res.data.data
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    }
  }
}
</script>
